<template>
	<div class="padding-container preview-wrap">
		<div class="title-part">
			<span>{{ $root.lang.control.app.list.title }}</span>
		</div>
		<el-form ref="tableParams" :inline="true" :model="tableParams" size="small">
      <el-form-item :label="$root.lang.control.app.list.appName">
        <el-input v-model="tableParams.appName"></el-input>
      </el-form-item>
      <el-form-item :label="$root.lang.control.app.list.appVersion">
        <el-input v-model="tableParams.appVersion"></el-input>
      </el-form-item>
      <el-form-item :label="$root.lang.control.app.list.deviceType">
        <el-select v-model="tableParams.deviceType" :placeholder="$root.lang.control.app.list.select" clearable>
          <el-option :label="item.typeName" :value="item.typeCode" :key="item.typeCode" v-for="item in deviceTypeList"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$root.lang.control.app.list.isPublic">
        <el-select v-model="tableParams.isPublic" :placeholder="$root.lang.control.app.list.select" clearable>
          <el-option :label="item.title" :value="item.value" :key="item.value" v-for="item in baseState.isPublic"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$root.lang.control.app.list.auditState">
        <el-select v-model="tableParams.auditState" :placeholder="$root.lang.control.app.list.select" clearable>
          <el-option :label="item.title" :value="item.value" :key="item.value" v-for="item in baseState.appAuditState"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$root.lang.control.app.list.delFlag">
        <el-select v-model="tableParams.delFlag" :placeholder="$root.lang.control.app.list.select">
          <el-option :label="item.title" :value="item.value" :key="item.value" v-for="item in baseState.delFlag"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="searchAction()" >{{ $root.lang.control.app.list.query }}</el-button>
        <el-button size="small" @click="searchAction(true)" >{{ $root.lang.control.app.list.empty }}</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="appList"
      :empty-text="$root.lang.control.app.list.noData"
      size="small"
      border
      style="width: 100%"
      >
      <el-table-column label=""  align="center" width="65">
        <template slot-scope="scope">
          <el-radio :label="scope.$index" v-model="appRadio"  @click.native.prevent="clickitem(scope.$index,scope.row)">&nbsp</el-radio>
        </template>
      </el-table-column>
      <el-table-column
        width="150"
        align="center"
        :label="$root.lang.control.app.list.appName">
        <template slot-scope="scope">
          <span>{{ scope.row.appName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        width="150"
        align="center"
        :label="$root.lang.control.app.list.appIdentification">
        <template slot-scope="scope">
          <span>{{ scope.row.appIdentification }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$root.lang.control.app.list.appVersion">
        <template slot-scope="scope">
          <span>{{ scope.row.appVersion }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        width="90"
        :label="$root.lang.control.app.list.appV">
        <template slot-scope="scope">
          <span>{{ scope.row.appV | dataFormat}}</span>
        </template>
      </el-table-column>
      <el-table-column
        width="240"
        align="center"
        :label="$root.lang.control.app.list.appMainClass">
        <template slot-scope="scope">
          <span>{{ scope.row.appMainClass }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$root.lang.control.app.list.deviceType">
        <template slot-scope="scope">
          <p>{{ scope.row.typeName }}</p>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$root.lang.control.app.list.isPublic">
        <template slot-scope="scope">
          <p>{{ baseState.isPublic, scope.row.isPublic, "--" | matchValue }}</p>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$root.lang.control.app.list.auditState">
        <template slot-scope="scope">
          <p>{{ baseState.appAuditState, scope.row.auditState, "--" | matchValue }}</p>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$root.lang.control.app.list.creUser">
        <template slot-scope="scope">
          <span v-if="scope.row.isPublic == 0">{{ scope.row.userName }}</span>
          <span v-else-if="scope.row.isPublic == 1">{{ scope.row.publicUserName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        width="140"
        align="center"
        :label="$root.lang.control.app.list.creTime">
        <template slot-scope="scope">
          <span v-if="scope.row.isPublic == 0">{{ scope.row.creTime | dateFormat }}</span>
          <span v-else-if="scope.row.isPublic == 1">{{ scope.row.publicCreTime | dateFormat }}</span>
        </template>
      </el-table-column>
      <el-table-column
        width="100"
        align="center"
        fixed="right"
        :label="$root.lang.control.app.list.operate">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="toDetail(scope.$index, scope.row)">{{ $root.lang.control.app.list.look }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-if="tableTotal>0"
      @size-change="sizeChange"
      @current-change="pageChange"
      @prev-click="pageChange"
      @next-click="pageChange"
      :current-page="tableParams.pageNum"
      :page-sizes="[10, 30, 50, 100]"
      :page-size="tableParams.pageSize"
      layout="total, sizes, prev, pager, next"
      :total="tableTotal">
    </el-pagination>

	</div>
</template>

<script>
import '@/options/baseState'
export default {
	name: 'AppList',
	data() {
		return {
			tableParams: {
				pageNum: 1,
				pageSize: 10,
				appVersion: null,
        appName: null,
        delFlag: 0,
        auditState: null,
        isPublic: null,
        deviceType: null,
        isNative: 0,
        isInner: true,
			},
			listLoading: false,
      appList: [],
      appRadio: null,
      tableTotal: 0,
      baseState: {},
      deviceTypeList: []
		}
	},
  props: {
  },
  watch: {
  },
  methods: {
    checkLang() {
      let langType = this.$root.getStorage('lang')
      if(langType == 'en') {
        this.baseState = enBaseState
      } else {
        this.baseState = baseState
      }
    },
    sizeChange(val) {
      this.tableParams.pageNum = 1
      this.tableParams.pageSize = val
      this.fetchData()
    },
    pageChange(val) {
      this.tableParams.pageNum = val
      this.fetchData()
    },
    clickitem (e, row) {
      this.appRadio = e
      // e == this.appRadio ? this.appRadio = null : this.appRadio = e
      if(this.appRadio != null) {
        this.$emit('receiveApp',{
          key: 'appId',
          value: row.id
        })
      }
    },
    toDetail(index, row) {
      this.$router.push({ name:'PrivateDetail', params:{ id: row.id } })
    },
    searchAction(reset) {
      if(reset) {
        this.tableParams = {
          pageNum: 1,
          pageSize: 10,
          appVersion: null,
          appName: null,
          delFlag: 0,
          auditState: null,
          isPublic: null,
          deviceType: null,
          isNative: 0,
          isInner: true,
        }
      } else {
        this.tableParams.pageNum = 1
      }
      this.fetchData()
    },
    fetchData() {
      let base = this
      this.listLoading = true
      this.request({
        url: '/application/private/page',
        method: 'post',
        data: this.$root.optionProcess(this.tableParams)
      }).then(response => {
        this.appList = response.rows
        this.tableTotal = response.total
        this.listLoading = false
      }).catch(error => {
        this.listLoading = false
        this.appList = []
      })
    },
    getDeviceType() {
      this.request({
        url: '/device/type/list',
        method: 'post',
        data: this.$root.optionProcess({
          delFlag: 0
        })
      }).then(response => {
        this.deviceTypeList = response.result
      }).catch(error => {
        this.$message({
          message: error.errmsg,
          type: 'error',
          duration: 3 * 1000
        })
      })
    }
  },
  created() {
    this.getDeviceType()
    this.checkLang()
    this.fetchData()
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
	.padding-container {
		text-align:left;
		.title-part {
			text-align: left;
			font-size:16px;
			padding-bottom:30px;
		}
    .el-radio__label {
      padding:0;
    }
	}
</style>